﻿$.validator.methods.equal = function (value, element, param) {
    return value == param;
};

$().ready(function () {

    $("#optSoftTissue").attr("class", "ListSelectedOption");
    $("#optDashboard").attr("class", "");
    $("#optGeneral").attr("class", "current");
    $("#optAccounting").attr("class", "");

    var validator = $("#frmSoftTissue").bind("invalid-form.validate", function () {
        $("#lblErrorMsgSoftTissue").show();
    }).validate({
        debug: true,
        rules: {
            txtSoftTissueAlterations: {
                required: true,
                minlength: 5
            },
        },
        messages: {
            txtSoftTissueAlterations: ""
        }
    });

    $('#btnClearSoftTissueForm').live('click', function () {
        ClearSoftTissueForm();
    });

    $('#btnAddSofttIssue').live('click', function () {
        IsValidSoftTissueForm();
    });

    $("#lblErrorMsgSoftTissue").hide();
    $("#lblsuccessMsgSoftTissue").hide();

    $("#txtSoftTissueAlterations").removeClass("error");
    $("#chkVisibleSofttIssue").removeClass("error");
});

function IsValidSoftTissueForm() {
    if ($("#frmSoftTissue").valid()) {
        $("#lblErrorMsgSoftTissue").hide();
        $("#lblsuccessMsgSoftTissue").hide();
        AddSoftTissue();
    }
}

function AddSoftTissue() {
    $.ajax(
    {
        type: "POST",
        url: "/ODSYSUI/General/AddSoftTissue",
        data:
            JSON.stringify({
                Alterations: $('#txtSoftTissueAlterations').val(),
                Visible: $('#chkVisibleSofttIssue').is(':checked')
            }),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
            if (result.success) {
                ClearSoftTissueForm();
                $("#lblsuccessMsgSoftTissue").show(300).delay(5000).hide(400);
            }
            else {
                $("#lblsuccessMsgSoftTissue").hide();
                $("#lblErrorMsgSoftTissue").show();
                $("#frmSoftTissueErrorMessage").text(result.msg);
            }
        },
        error: function (req, status, error) {
            $("#lblsuccessMsgSoftTissue").hide();
            $("#lblErrorMsgSoftTissue").show();
            $("#frmSoftTissueErrorMessage").text("An error has occurred. Please contact your administrator.");
        }
    });
}

function ClearSoftTissueForm() {
    $('#txtSoftTissueAlterations').val('');
    $('#chkVisibleSofttIssue').attr('checked', false);

    $("#lblErrorMsgSoftTissue").hide();
    $("#lblsuccessMsgSoftTissue").hide();

    $("#txtSoftTissueAlterations").removeClass("error");
    $("#chkVisibleSoftTissue").removeClass("error");
}